package gr.nouki.socialparkingfinder;

import gr.nouki.socialparkingfinder.database.DatabaseHelper;
import gr.nouki.socialparkingfinder.database.domain.Driver;

import java.util.List;
import java.util.Random;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;
import com.j256.ormlite.dao.RuntimeExceptionDao;

public class MainActivity extends OrmLiteBaseActivity<DatabaseHelper> {

	private final String LOG_TAG = getClass().getSimpleName();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Remove title bar
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		Log.i(LOG_TAG,
				"creating " + getClass() + " at " + System.currentTimeMillis());

		// Remove notification bar
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		initDB("onCreate");
		setContentView(R.layout.activity_main);
		// signup
		final Button signup = (Button) findViewById(R.id.loginButtonId);
		signup.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				startActivity(new Intent(MainActivity.this,
						SignUpActivity.class));
			}
		});
		// signin
		final Button signin = (Button) findViewById(R.id.signinButtonId);
		signin.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				startActivity(new Intent(MainActivity.this,
						SignInActivity.class));
			}
		});
	}

	private void initDB(String action) {
		// get our dao
		RuntimeExceptionDao<Driver, Integer> driverDao = getHelper()
				.getDriverDao();
		// query for all of the data objects in the database
		List<Driver> list = driverDao.queryForAll();
		// our string builder for building the content-view
		StringBuilder sb = new StringBuilder();
		sb.append("got ").append(list.size()).append(" entries in ")
				.append(action).append("\n");

		// if we already have items in the database
		int driverC = 0;
		for (Driver driver : list) {
			sb.append("------------------------------------------\n");
			sb.append("[").append(driverC).append("] = ").append(driver)
					.append("\n");
			driverC++;
		}
		sb.append("------------------------------------------\n");
		for (Driver driver : list) {
			driverDao.delete(driver);
			sb.append("deleted id ").append(driver.getId()).append("\n");
			Log.i(LOG_TAG, "deleting driver(" + driver.getId() + ")");
			driverC++;
		}

		// create a new driver object
		long millis = System.currentTimeMillis();
		Driver driver = new Driver();
		driver.setUsername("maria");
		driver.setPassword("maria");
		// store it in the database
		driverDao.create(driver);

		driver = new Driver();
		driver.setUsername("babis");
		driver.setPassword("babis");
		// store it in the database
		driverDao.create(driver);
		Log.i(LOG_TAG, "created driver(" + millis + ")");
	}
}
